﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using K.ClientPublic;
using K.Contract;
using K.EnterpriseLibary;
using System.ServiceModel;



namespace K.ClientOfWinForm
{
    public partial class MainWindow : Form
    {
        private string _ExceptionDisposeServiceName;
        private string _CartonServiceName;
        private string _TestServerServiceName;

        public IServerThread ServerThread;
        public MainWindow()
        {
            InitializeComponent();
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
        }

        private void ShowAlertWindow()
        {
            K.ClientOfWinForm.AlertWindow AlertWindow = new AlertWindow();
            AlertWindow.ShowDialog();
        }

        private void ShowExceptionWindow(Exception exp)
        {
            ExceptionWindow NewExceptionWindow = new ExceptionWindow();
            NewExceptionWindow.Exception = exp;
            NewExceptionWindow.ShowDialog();
        }

        private void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            ExceptionPublic.ClientExceptionProcess(e.Exception, ShowExceptionWindow);
        }


        private void Main_Load(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
            timer1.Start();
            LoginWindow Login = new LoginWindow();
            if (Login.ShowDialog() == DialogResult.OK)
            {
                //此处可接受由登录界面传送过来的值  
                _ExceptionDisposeServiceName = Login.ExceptionDisposeServiceName;
                _CartonServiceName = Login.CartonServiceName;
                _TestServerServiceName = Login.TestServerServiceName;

            }
            //IExceptionDispose ExceptionDispose = K.ClientPublic.RemotingPublic.CreateChannel(_ExceptionDisposeServiceName);
            IExceptionDispose ExceptionDispose = K.ClientPublic.RemotingPublic.CreateChannel<IExceptionDispose>(_ExceptionDisposeServiceName);
            double addResult = ExceptionDispose.Add(2, 3);

            PackingPublic.Initialize(() => K.ClientPublic.RemotingPublic.CreateChannel<ICartonProcess>(_CartonServiceName));
            //MessageBox.Show(addResult.ToString());

            //Carton NewCarton = new Carton();
            //NewCarton.Width = 13;
            //NewCarton.Length = 98;
            //NewCarton.Height = 40;
            //NewCarton.Weight = 45;
            //NewCarton.AutoDamage();


            

            //Test
            //DataTable dt = new DataTable();
            //DataRow[] dr = new DataRow[6];
            //dr.
            //int[] intArray = new int[6];
            //intArray.m
           
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (ServerThread != null)
                lb_TestServer.Text = ServerThread.GetI().ToString();
        }

        private void bt_StartServer_Click(object sender, EventArgs e)
        {
            ServerThread = K.ClientPublic.RemotingPublic.CreateChannel<IServerThread>(_TestServerServiceName);
            ServerThread.DoThread();
  
        }

        private void bt_StopServer_Click(object sender, EventArgs e)
        {
            ServerThread.SetExsits();
        }

        private void btException_Click(object sender, EventArgs e)
        {
            //throw new KException("test");
            throw new Exception("test");
        }      
    }


}
